#!/bin/bash
# Run the regression tests

###SHELLPACK preamble xfstests-bench git

###SHELLPACK parseargBegin
###SHELLPACK parseargEnd
###SHELLPACK monitor_hooks

###SHELLPACK check_install_required xfstests-${VERSION}
###SHELLPACK init_complete

# Check configuration

# FIXME: we need to umount and mount the test device and mmtests use the
# subdirectories (srouces, installed, tmp), umount is blocked, lots of scripts
# depend on SHELLPACK_TEMP etc, so we have to restrict xfstests until the rest
# gets reviewed or a better fix is found
if [ "$TESTDISK_PARTITION" != "" ]; then
	die "TESTDISK_PARTITION must not be set for xfstests, use XFSTESTS_TEST_DEV"
fi
if [ "$XFSTESTS_TEST_DEV" = "" ]; then
	die XFSTESTS_TEST_DEV must be configured

fi

if [ -z "$XFSTESTS_SCRATCH_DEV" -a -z "$XFSTESTS_SCRATCH_DEV_POOL" ]; then
	warn "XFSTESTS_SCRATCH_DEV or XFSTESTS_SCRATCH_DEV_POOL should be configured"
fi
if [ "$TESTDISK_FILESYSTEM" = "" ]; then
	die TESTDISK_FILESYSTEM must be configured
fi

# Configure environment
export TEST_DEV=$XFSTESTS_TEST_DEV
export TEST_DIR=$SHELLPACK_TOPLEVEL/test_dir
# exactly one of the SCRATCH_DEV* variables can be set, but we'll not validate
# it here and let xfstests do that
if [ "$SCRATCH_DEV" != "" ]; then
	export SCRATCH_DEV=$XFSTESTS_SCRATCH_DEV
	export SCRATCH_DEV_POOL=$XFSTESTS_SCRATCH_DEV_POOL
	export SCRATCH_MNT=$SHELLPACK_TOPLEVEL/scratch_mnt
	mkdir -p $SCRATCH_MNT
fi
export FSTYP=$TESTDISK_FILESYSTEM
export MKFS_OPTIONS=$TESTDISK_MKFS_PARAM
export MOUNT_OPTIONS=${TESTDISK_MOUNT_ARGS:+-o "$TESTDISK_MOUNT_ARGS"}

mkdir -p $TEST_DIR

# FIXME: as we're not using mmtests-provided mkfs step, do that here manually
mkfs."$TESTDISK_FILESYSTEM" $TESTDISK_MKFS_PARAM $XFSTESTS_TEST_DEV ||
	die Unable to mkfs TEST_DEV

monitor_pre_hook $LOGDIR_RESULTS $P

./check -T $XFSTESTS_ARGS 2>&1 | tee $LOGDIR_RESULTS/xfstests-default.log \
	|| die Failed to run xfstests

monitor_post_hook $LOGDIR_RESULTS $P

exit $SHELLPACK_SUCCESS
